package edu.unf.cen6940.service;

import edu.unf.cen6940.dao.ThesisDAO;
import edu.unf.cen6940.dao.ThesisstatusDAO;

import edu.unf.cen6940.domain.Thesis;
import edu.unf.cen6940.domain.Thesisstatus;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Thesisstatus entities
 * 
 */

@Service("ThesisstatusService")
@Transactional
public class ThesisstatusServiceImpl implements ThesisstatusService {

	/**
	 * DAO injected by Spring that manages Thesis entities
	 * 
	 */
	@Autowired
	private ThesisDAO thesisDAO;

	/**
	 * DAO injected by Spring that manages Thesisstatus entities
	 * 
	 */
	@Autowired
	private ThesisstatusDAO thesisstatusDAO;

	/**
	 * Instantiates a new ThesisstatusServiceImpl.
	 *
	 */
	public ThesisstatusServiceImpl() {
	}

	/**
	 * Delete an existing Thesis entity
	 * 
	 */
	@Transactional
	public Thesisstatus deleteThesisstatusThesises(Integer thesisstatus_thesisStatusId, Integer related_thesises_thesisId) {
		Thesisstatus thesisstatus = thesisstatusDAO.findThesisstatusByPrimaryKey(thesisstatus_thesisStatusId, -1, -1);
		Thesis related_thesises = thesisDAO.findThesisByPrimaryKey(related_thesises_thesisId, -1, -1);

		thesisstatus.getThesises().remove(related_thesises);
		related_thesises.getThesisstatuses().remove(thesisstatus);
		thesisstatus = thesisstatusDAO.store(thesisstatus);
		thesisstatusDAO.flush();

		related_thesises = thesisDAO.store(related_thesises);
		thesisDAO.flush();

		thesisDAO.remove(related_thesises);
		thesisDAO.flush();

		return thesisstatus;
	}

	/**
	 * Load an existing Thesisstatus entity
	 * 
	 */
	@Transactional
	public Set<Thesisstatus> loadThesisstatuss() {
		return thesisstatusDAO.findAllThesisstatuss();
	}

	/**
	 * Save an existing Thesisstatus entity
	 * 
	 */
	@Transactional
	public void saveThesisstatus(Thesisstatus thesisstatus) {
		Thesisstatus existingThesisstatus = thesisstatusDAO.findThesisstatusByPrimaryKey(thesisstatus.getThesisStatusId());

		if (existingThesisstatus != null) {
			if (existingThesisstatus != thesisstatus) {
				existingThesisstatus.setThesisStatusId(thesisstatus.getThesisStatusId());
				existingThesisstatus.setProposed(thesisstatus.getProposed());
				existingThesisstatus.setProposalDate(thesisstatus.getProposalDate());
				existingThesisstatus.setDefended(thesisstatus.getDefended());
				existingThesisstatus.setDefenseDate(thesisstatus.getDefenseDate());
				existingThesisstatus.setProposalPass(thesisstatus.getProposalPass());
				existingThesisstatus.setDefensePass(thesisstatus.getDefensePass());
				existingThesisstatus.setComments(thesisstatus.getComments());
			}
			thesisstatus = thesisstatusDAO.store(existingThesisstatus);
		} else {
			thesisstatus = thesisstatusDAO.store(thesisstatus);
		}
		thesisstatusDAO.flush();
	}

	/**
	 * Return a count of all Thesisstatus entity
	 * 
	 */
	@Transactional
	public Integer countThesisstatuss() {
		return ((Long) thesisstatusDAO.createQuerySingleResult("select count(o) from Thesisstatus o").getSingleResult()).intValue();
	}

	/**
	 * Return all Thesisstatus entity
	 * 
	 */
	@Transactional
	public List<Thesisstatus> findAllThesisstatuss(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Thesisstatus>(thesisstatusDAO.findAllThesisstatuss(startResult, maxRows));
	}

	/**
	 * Delete an existing Thesisstatus entity
	 * 
	 */
	@Transactional
	public void deleteThesisstatus(Thesisstatus thesisstatus) {
		thesisstatusDAO.remove(thesisstatus);
		thesisstatusDAO.flush();
	}

	/**
	 */
	@Transactional
	public Thesisstatus findThesisstatusByPrimaryKey(Integer thesisStatusId) {
		return thesisstatusDAO.findThesisstatusByPrimaryKey(thesisStatusId);
	}

	/**
	 * Save an existing Thesis entity
	 * 
	 */
	@Transactional
	public Thesisstatus saveThesisstatusThesises(Integer thesisStatusId, Thesis related_thesises) {
		Thesisstatus thesisstatus = thesisstatusDAO.findThesisstatusByPrimaryKey(thesisStatusId, -1, -1);
		Thesis existingthesises = thesisDAO.findThesisByPrimaryKey(related_thesises.getThesisId());

		// copy into the existing record to preserve existing relationships
		if (existingthesises != null) {
			existingthesises.setThesisId(related_thesises.getThesisId());
			existingthesises.setTitle(related_thesises.getTitle());
			existingthesises.setAbstract_(related_thesises.getAbstract_());
			existingthesises.setProposalAttempts(related_thesises.getProposalAttempts());
			existingthesises.setDefenseAttempts(related_thesises.getDefenseAttempts());
			existingthesises.setStartDate(related_thesises.getStartDate());
			existingthesises.setDoi(related_thesises.getDoi());
			related_thesises = existingthesises;
		} else {
			related_thesises = thesisDAO.store(related_thesises);
			thesisDAO.flush();
		}

		thesisstatus.getThesises().add(related_thesises);
		related_thesises.getThesisstatuses().add(thesisstatus);
		thesisstatus = thesisstatusDAO.store(thesisstatus);
		thesisstatusDAO.flush();

		related_thesises = thesisDAO.store(related_thesises);
		thesisDAO.flush();

		return thesisstatus;
	}
}
